<?php
class Server {
  public static function submit_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    if ($req->name == '') {
      echo 'Name is empty';
      return;
    }
    
    if (isset($req->__nofile) && $req->__nofile == 1) {
      $attr = $req->__attr;
      if (file_exists($req->$attr)) {
        unlink($req->$attr);
      }
      unset($req->__nofile);
      unset($req->__attr);
      $req->$attr = '';
    }
    
    if (isset($req->id)) {
      $orm->update('pb_ht_category', $req);
    } else {
      $orm->add('pb_ht_category', $req);
      $req->id = $orm->last_id();
    }
    
    if (isset($req->__pg)) {
      $req->__objs = $orm->get('pb_ht_category', NULL, NULL, NULL, NULL, NULL)->d;
    }
    
    echo json_encode($req);
  }
  
  public static function update_objs($orm, $sys, $inst, $is_iframe) {
    $reqs = json_decode(Utils::get_param('reqs', '[]'));
    foreach ($reqs as &$itm) {
      $orm->update('pb_ht_category', $itm);
    }
  }
  
  public static function delete_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    Server::__delete_obj($orm, $req);
    echo json_encode($req);
  }
  
  public static function __delete_obj($orm, $req) {
    $opts = new stdClass();
    $opts->cd = 'parent_id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $rslt = $orm->get('pb_ht_category', NULL, NULL, NULL, NULL, $opts)->d;
    foreach ($rslt as &$itm) {
      Server::__delete_obj($orm, $itm);
    }
    $orm->delete('pb_ht_category', $req);
  }
}
?>